<?php

namespace api\controller;

// +----------------------------------------------------------------------
// | 部门数据控制层
// +----------------------------------------------------------------------

use common\controller\VerifyController;
use common\exception\LogicException;
use common\service\DepartmentService;
use think\Request;
use think\Response;

class DepartmentController extends VerifyController
{
    /**
     * @var \common\service\DepartmentService
     */
    protected $departmentService;


    public function __construct()
    {
        parent::__construct();
        $this->departmentService = new DepartmentService();
    }

    /**
     * 创建部门
     * @param Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function create(Request $request)
    {
        try {
            $param = $request->param();
            $resData = $this->departmentService->create($param['data']);
            return json(success_response('Department successfully created.', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 删除部门
     * @param Request $request
     * @return \think\Response
     * @throws \think\Exception|\Throwable
     */
    public function delete(Request $request)
    {
        try {
            $param = $request->param();
            $resData = $this->departmentService->delete($param['param']);
            return json(success_response('Department successfully created.', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    public function getParentDepartmentList(Request $request)
    {
        try {
            $param = $request->param();
            $resData = $this->departmentService->getParentDepartmentList($param['param']);
            return json(success_response('', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 部门人员统计
     * @param Request $request
     * @return \think\Response
     */
    public function departmentUserCount(Request $request)
    {
        try {
            $param = $request->param();
            $includeEmployed = $param['param']['include_employed'] ?? 'no';
            $resData = $this->departmentService->departmentUserCount($param['param'], $request->getTenantId(), $includeEmployed === 'yes');
            return json(success_response('', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 基础查询
     * @param Request $request
     * @return \think\Response
     */
    public function baseSelect(Request $request)
    {
        $param = $request->param()['param'];
        $tenantId = $param["filter"]['tenant_id'] ?? $request->getTenantId();
        $resData = $this->departmentService->baseSelect($param, $tenantId);
        return json(success_response('Department successfully created.', $resData));
    }

    /**
     * 部门用户查询
     * @param Request $request
     * @return \think\Response
     */
    public function selectUserList(Request $request)
    {
        try {
            $param = $request->param();
            $resData = $this->departmentService->selectUserList($param['param']);
            return json(success_response('OK', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 部门人员统计
     * @param Request $request
     * @return Response
     * @throws \Exception
     */
    public function departmentUserTree(Request $request)
    {
        try {
            $param = $request->param();
            $includeEmployed = $param['param']['include_employed'] ?? 'no';
            $resData = $this->departmentService->departmentUserTree($param['param'], $request->getTenantId(), $includeEmployed === 'yes');
            return json(success_response('', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 查询当前的部门基础信息
     * @param  Request  $request
     * @return Response
     */
    public function getDepartmentList(Request $request)
    {
        $param = $request->param()['param'];
        $result = $this->departmentService->getDepartmentList($param);
        return json(success_response('', $result));
    }
}
